n, m = map(int, raw_input().split())
print (pow(3, n, m) - 1) % m
